<!DOCTYPE html>
<html>
  
<head>
  <meta charset="utf-8">
  <meta name="author" content="饿包子" />
  
  
  <title>CSS变量的历史和兼容性 | 饿包子博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="CSS," />
  

  
  <meta name="description" content="自1998年5月起，Web社区就有要求css定义变量的方法，在我看来，这是很正常的，一个新的功能不可能是一时的冲动，拍拍屁股就决定的，它需要的是不断的检测，不断的改正，时间是最公正的裁判不是吗？">

  
  
    <link rel="icon" href="/blog/images/xian.ico">
    <link rel="apple-touch-icon" href="/blog/images/Logo.png">
  

  
<link rel="stylesheet" href="/blog/css/Awesome.min.css">


  
<link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">


  
  
<script src="/blog/common/jquery.min.js"></script>

  
    
<script src="/blog/common/Av.min.js"></script>

  

  
    
<script src="/blog/common/MathJax.js"></script>

  

  
    
<script src="/blog/common/Valine.min.js"></script>

  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"gE1EosDsVgB2slV4E92yPkcG-gzGzoHsz","appkey":"niR787UVTMyJQrR6gmOiJCKe","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2018-06-10",
    passwords: ["a621ab606db2a11f63edc576a729843b8269250dc324206871d90635ac5e531c", ],
    is_post: true,
    lock: false,
    author: "饿包子",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":false},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  
<script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">清风逐月</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 我思故我在</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">主页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">日记</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/lixianbin1/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            主页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">
            日记
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2018-06-03
    </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
    <span>
       | <i class="fa fa-calendar"></i>更新时间:NaN-NaN-NaN NaN:NaN 
    </span>
  </div>
  <h1 class="passage-title">
    CSS变量的历史和兼容性
  </h1>
  
    <div class="passage-cover">
      <figure style="background-image:url(/blog/images/posts/1911051.jpg);"></figure>
    </div>
  
  <article class="passage-article">
    <h3 id="CSS变量的历史"><a href="#CSS变量的历史" class="headerlink" title="CSS变量的历史"></a>CSS变量的历史</h3><p>自1998年5月起，Web社区就有要求css定义变量的方法，在我看来，这是很正常的，一个新的功能不可能是一时的冲动，拍拍屁股就决定的，它需要的是不断的检测，不断的改正，时间是最公正的裁判不是吗？</p>
<p>2011年2月，css变量草案被提出，它最初受到Dave Hyatt和Daniel Glazman最初编写的<a target="_blank" rel="noopener" href="http://disruptive-innovations.com/zoo/cssvariables/">变量提案</a>的启发，后经过一系列的讨论及改正，至2014年5月，<a target="_blank" rel="noopener" href="https://www.w3.org/TR/css-variables-1/">CSS变量草案</a>快速的成长起来，变的更加的健壮。</p>
<h3 id="CSS变量的声明"><a href="#CSS变量的声明" class="headerlink" title="CSS变量的声明"></a>CSS变量的声明</h3><p>首先你需要制定变量的作用域，全局或者是局部，其次你需要使用<code>--</code>充当变量前缀 ，就像sass的<code>$</code>和lass的<code>@</code>，它们的用法相差不大。注意，CSS变量对大小写敏感，变量的值可以是颜色、字符串、多个值的组合等。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">:root&#123;</span><br><span class="line">    --colorText:blue;</span><br><span class="line">    --textContent:&quot;Hello&quot;;</span><br><span class="line">    --varG:var(--colorText);</span><br><span class="line">    --number:10;</span><br><span class="line">&#125;</span><br><span class="line">#body&#123;</span><br><span class="line">    --only:green;</span><br><span class="line">&#125;</span><br><span class="line">#body&#123;</span><br><span class="line"></span><br><span class="line">    /*var()用于读取变量，var()可以有两个参数，第二个为默认值（可选），即变量失效后使用的值*/</span><br><span class="line">    color:var(colorText,green)；</span><br><span class="line">    </span><br><span class="line">    /*变量是数字，在涉及单位的时候，不能直接使用，需要calc()进行转换，单位不能作为字符串存储*/</span><br><span class="line">    width:calc(var(--number) * px)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="CSS变量的优先级"><a href="#CSS变量的优先级" class="headerlink" title="CSS变量的优先级"></a>CSS变量的优先级</h3><p>当css变量在多个选择器内进行声明的时候，读取的时候，优先级最高的生效，优先级规则参照css层叠规则<br>就个人建议，变量应尽量 :root 作用域下进行声明，便于后来者的样式维护</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">:root &#123; --color: blue; &#125;</span><br><span class="line">div &#123; --color: green; &#125;</span><br><span class="line">#alert &#123; --color: red; &#125;</span><br><span class="line">* &#123; color: var(--color); &#125;</span><br><span class="line"></span><br><span class="line">&lt;p&gt;I inherited blue from the root element!&lt;/p&gt;</span><br><span class="line">&lt;div&gt;I got green set directly on me!&lt;/div&gt;</span><br><span class="line">&lt;div id=&#x27;alert&#x27;&gt;</span><br><span class="line">  While I got red set directly on me!</span><br><span class="line">  &lt;p&gt;I’m red too, because of inheritance!&lt;/p&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>

<h3 id="CSS变量在响应式的应用"><a href="#CSS变量在响应式的应用" class="headerlink" title="CSS变量在响应式的应用"></a>CSS变量在响应式的应用</h3><p>在依靠媒体查询的响应式的时候，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">body &#123;</span><br><span class="line">    --colorText: red;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@media screen and (min-width: 768px) &#123;</span><br><span class="line">    body &#123;</span><br><span class="line">        --colorText: red;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="CSS变量在其他方面的应用"><a href="#CSS变量在其他方面的应用" class="headerlink" title="CSS变量在其他方面的应用"></a>CSS变量在其他方面的应用</h3><p>就像阮一峰大佬说的那样，CSS 变量提供了 JavaScript 与 CSS 通信的一种途径。途径有很多，目的却只有一个，简化操作，强化功能，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">// 设置变量</span><br><span class="line">document.body.style.setProperty(&#x27;--primary&#x27;, &#x27;#7F583F&#x27;);</span><br><span class="line"></span><br><span class="line">// 读取变量</span><br><span class="line">document.body.style.getPropertyValue(&#x27;--primary&#x27;).trim();</span><br><span class="line">// &#x27;#7F583F&#x27;</span><br><span class="line"></span><br><span class="line">// 删除变量</span><br><span class="line">document.body.style.removeProperty(&#x27;--primary&#x27;);</span><br></pre></td></tr></table></figure>

<h3 id="CSS变量的兼容性表现"><a href="#CSS变量的兼容性表现" class="headerlink" title="CSS变量的兼容性表现"></a>CSS变量的兼容性表现</h3><p>由于国内和国外的社会环境和web环境的不同，导致CSS变量在国内出现了尴尬的局面，使得CSS变量在某种程度上成为了食之无味，弃之可惜的“鸡肋”。在功能性方面，它比不上Sass，less已及stylus，在兼容性方面，微软在2017年3月宣布，Edge 浏览器将支持 CSS 变量。然而国内公司虽然大多数已经不要求兼容IE9以下的IE浏览器，但是 IE9+<br>依然不支持CSS变量</p>
<iframe src="https://caniuse.com/css-variables/embed" width="100%" height="495px"></iframe>

<p>CSS样式的兼容性的办法也是有的，你可以像下面这样写</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">div&#123;</span><br><span class="line">    color:red;</span><br><span class="line">    /*如果都这样写了，那这还有什么意义，我为什么要写下面这个*/</span><br><span class="line">    --divColor:red;</span><br><span class="line">    color:var(--divColor);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/*使用CSS3的功能查询*/</span><br><span class="line">@supports ( (--a: 0)) &#123;</span><br><span class="line">  /* 兼容 */</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@supports ( not (--a: 0)) &#123;</span><br><span class="line">  /* 不兼容 */</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>JavaScript检测兼容性</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">const isSupported =</span><br><span class="line">  window.CSS &amp;&amp;</span><br><span class="line">  window.CSS.supports &amp;&amp;</span><br><span class="line">  window.CSS.supports(&#x27;--a&#x27;, 0);</span><br><span class="line"></span><br><span class="line">if (isSupported) &#123;</span><br><span class="line">  console.log(&#x27;兼容&#x27;)</span><br><span class="line">&#125; else &#123;</span><br><span class="line">  console.log(&#x27;不兼容&#x27;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>规则：<a target="_blank" rel="noopener" href="https://www.w3.org/TR/css-variables-1/">CSS自定义属性文档</a></p>
<p>总结：就目前而言，在国内的环境之下，使用CSS变量有些鸡肋的表现，当然，仁者见仁智者见智，这只是我个人的想法</p>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%8F%98%E9%87%8F%E7%9A%84%E5%8E%86%E5%8F%B2"><span class="toc-text">CSS变量的历史</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%8F%98%E9%87%8F%E7%9A%84%E5%A3%B0%E6%98%8E"><span class="toc-text">CSS变量的声明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%8F%98%E9%87%8F%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">CSS变量的优先级</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%8F%98%E9%87%8F%E5%9C%A8%E5%93%8D%E5%BA%94%E5%BC%8F%E7%9A%84%E5%BA%94%E7%94%A8"><span class="toc-text">CSS变量在响应式的应用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%8F%98%E9%87%8F%E5%9C%A8%E5%85%B6%E4%BB%96%E6%96%B9%E9%9D%A2%E7%9A%84%E5%BA%94%E7%94%A8"><span class="toc-text">CSS变量在其他方面的应用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%8F%98%E9%87%8F%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E8%A1%A8%E7%8E%B0"><span class="toc-text">CSS变量的兼容性表现</span></a></li></ol>
  </div>
</aside>

  
  
    <aside class="passage-copyright">
      <div>本文作者: 饿包子</div>
      
        <div>
          原文链接: 
          <a href="" target="_blank">https://lixianbin1.github.io/blog/2018/06/03/CSS%E5%8F%98%E9%87%8F%E7%9A%84%E5%8E%86%E5%8F%B2%E5%92%8C%E5%85%BC%E5%AE%B9%E6%80%A7/</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/blog/tags/CSS/"><i class="fa fa-tags"></i>CSS</a>
    
    </div>
  
</div>

    </main>
    
      
<div class="site-comment-contanier" data-plateform="leancloud">
  
    <p id="site-comment-info">
      <i class="fa fa-spinner fa-spin"></i> 评论加载中
    </p>
    <div id="site-comment"></div>
  
</div>
    
    
<div class="site-footer-wrapper">
<!--
  <div class="footer-top">
    <a class="left" href="/blog/2018/09/04/%E5%85%B3%E4%BA%8E%E5%B1%8F%E5%B9%95%E9%80%82%E9%85%8D%E7%9A%84%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a class="right" href="/blog/2018/06/01/Hexo%E5%8F%8Amarkdown%E7%9A%84%E8%AF%AD%E6%B3%95%E5%8F%82%E8%80%83/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
-->
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">网站推荐</h5>
          
            <span class="site-footer-item">
              <a href="https://tympanus.net/codrops/" target="_blank">Codrops</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://taobaofed.org/" target="_blank">淘宝前端团队(FED)</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">文档教程</h5>
          
            <span class="site-footer-item">
              <a href="https://es6.ruanyifeng.com/" target="_blank">ES6入门</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://developer.mozilla.org/zh-CN/docs/Web" target="_blank">MDN Web文档</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">兴趣前沿</h5>
          
            <span class="site-footer-item">
              <a href="https://threejs.org/" target="_blank">threejs</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://github.com/justadudewhohacks/face-api.js" target="_blank">face-api</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: xianbin.me@qq.com
      </div>
    
<!--     <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div> -->
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">

  <div class="bottom-bar-left">
<!--
    <a href="/blog/2018/09/04/%E5%85%B3%E4%BA%8E%E5%B1%8F%E5%B9%95%E9%80%82%E9%85%8D%E7%9A%84%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/2018/06/01/Hexo%E5%8F%8Amarkdown%E7%9A%84%E8%AF%AD%E6%B3%95%E5%8F%82%E8%80%83/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
-->
  </div>

  
  
  
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
</div>
    





    
  </body>
</html>